<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/3/30
 * Time: 9:07
 */

namespace app\home\controller;


class Loupan extends Base
{
    public function _initialize(){
        parent::_initialize();
        $this->assign('action',strtolower(request()->action()));
        $id = input('param.id/d',0);
        if(!$id){
            $id = input('param.house_id/d',0);
        }
        $this->assign('id',$id);
    }
    //楼盘列表页
    public function index(){
        $this->search();
        $this->getAttr();
        $this->getLists();
        $ads  = action('Poster/index',['id'=>2,'num'=>2],'event');//切换图广告
        $this->assign('price',$this->getPrice());
        $this->assign('ads',$ads);
        return $this->fetch();
    }
    //户型页面
    public function huxing(){
        $this->getAttr();
        $this->getHuxingLists();
        $ads  = action('Poster/index',['id'=>2,'num'=>2],'event');//切换图广告
        $this->assign('ads',$ads);
        $this->assign('price',getHuxingPrice());
        $this->assign('roomType',getRoom());
        return $this->fetch();
    }
    //户型搜索
    public function huxingsearch(){
        $param['area']       = input('param.area',0);
        $param['price']      = input('param.price',0);
        $param['room']       = input('param.room',0);//户型
        $param['renovation'] = input('param.renovation',0);//装修情况
        $param['sort']       = input('param.sort/d',0);//排序
        $keyword             = input('get.keyword');
        $data['h.status']      = 1;
        $data['l.city']        = $this->cityInfo['id'];
        if(!empty($param['area'])){
            $data['l.area'] = $param['area'];
        }
        if(!empty($param['price'])){
            $data['h.price'] = getHuxingPrice($param['price']);
        }
        if(!empty($param['room'])){
            $data['h.room'] = getRoom($param['room']);
        }
        if(!empty($param['renovation'])){
            $data['l.zhuangxiu_id'] = $param['renovation'];
        }
        $keyword && $data['l.title|h.title'] = ['like','%'.$keyword.'%'];
        $this->assign('param',$param);
        $this->assign('search',$param);
        return $data;
    }
    //户型列表
    public function getHuxingLists(){
        $where = $this->huxingsearch();
        $sort  = $this->huxingorder();
        $field = 'h.id,h.house_id,h.img,h.title,h.price,h.special,h.jianzhu_area,l.title as house_name,l.address,l.tags_name';
        $join = [
            ['house l','h.house_id=l.id']
        ];
        $lists = \think\Db::name('house_type')->alias('h')->join($join,null,'left')->where($where)->order($sort)->field($field)->paginate(10);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
    }
    //楼盘详细页
    public function detail(){
        $id   = input('param.id/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        model('house')->where(['id'=>$id])->setInc('hits');
        $this->getHousePic($id);
        $this->setSeo($info);
        $this->getProperty($id);
        $this->getRoomType($id);
        $this->getPicture($id);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //详细资料
    public function info(){
        $id   = input('param.house_id/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $this->getProperty($id);
        $info['seo_title'] .='详细资料';
        $this->setSeo($info);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //在售户型
    public function type(){
        $id   = input('param.house_id/d',0);
        $type_id = input('param.type_id/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $info['seo_title'] .='在售户型';
        $this->setSeo($info);
        $this->getRoomType($id,$type_id);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //户型详细
    public function typeShow(){
        $id        = input('param.house_id/d',0);
        $type_id   = input('param.id/d',0);
        $type_info = '';
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        if($type_id){
            $type_info = model('house_type')->where(['id'=>$type_id])->find();
        }
        $info['seo_title'] .=$type_info['title'];
        $this->setSeo($info);
        $this->assign('type_info',$type_info);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //相册
    public function pic(){
        $id   = input('param.house_id/d',0);
        $cate_id = input('param.cate/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $pic_cate = cache('linkmenu_10');
        $cate     = $pic_cate['cate'];
        $count    = '';
        if($cate){
            $field = '';
            foreach($cate as $v){
                $field  .= "count(case when cate_id={$v['id']} then cate_id end) as cate_{$v['id']},";
            }
            $field .= "count(id) as total";
            $count = model('house_pic')->field($field)->where(['house_id'=>$id,'status'=>1])->find();
        }
        $where = 'status=1 and house_id='.$id;
        $cate_id && $where .= ' and cate_id='.$cate_id;
        $info['seo_title'] .='相册';
        $this->setSeo($info);
        $this->assign('where',$where);
        $this->assign('cate_id',$cate_id);
        $this->assign('picCate',$cate);
        $this->assign('count',$count);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //新闻
    public function news(){
        $id   = input('param.house_id/d',0);
        $cate_id = input('param.cate/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $where = 'status=1 and house_id = '.$info['id'];
        $cate_id && $where .= " and cate_id=".$cate_id;

        $this->getNearHouse($info['lat'],$info['lng'],$info['city'],$info['area']);
        $info['seo_title'] .='资讯';
        $this->setSeo($info);
        $this->assign('info',$info);
        $this->assign('cate_id',$cate_id);
        $this->assign('where',$where);
        return $this->fetch();
    }
    //楼盘配套
    public function peitao(){
        $id   = input('param.house_id/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $info['seo_title'] = $info['seo_title'].'配套信息';
        $this->setSeo($info);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //问答
    public function question(){
        $id   = input('param.house_id/d',0);
        $info = $this->getHouseInfo($id);
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $info['seo_title'] = $info['seo_title'].'问答';
        $cate = cache('askCate');
        $this->setSeo($info);
        $this->getNearHouse($info['lat'],$info['lng'],$info['city'],$info['area']);
        $this->assign('info',$info);
        $this->assign('ask_cate',$cate['tree']);
        return $this->fetch();
    }
    private function getNearHouse($lat,$lng,$city=0,$area=0){
        $obj        = model("house");
        if($lat && $lng){
            $point      = "*,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$lat}*PI()/180-lat*PI()/180)/2),2)+COS({$lat}*PI()/180)*COS(lat*PI()/180)*POW(SIN(({$lng}*PI()/180-lng*PI()/180)/2),2)))*1000) as distance";
            $bindsql    = $obj->field($point)->select(false);
            $fields_res = 'id,title,average_price,distance';
            $where      = ['status'=>1,'distance'=>['lt',1000]];
            $lists      = $obj->table('('.$bindsql.') d')->field($fields_res)->where($where)->select();
        }else{
            $where['status'] = 1;
            $city && $where['city'] = $city;
            $area && $where['area'] = $area;
            $lists = $obj->where($where)->field('id,title,average_price')->limit(5)->select();
        }
       $this->assign('nearHouse',$lists);
    }
    private function getPicture($id){
        $cate = cache('linkmenu_10');
        $lists = model('house_pic')->where(['house_id'=>$id,'status'=>1])->limit(6)->select();
        $this->assign('picCate',$cate['cate']);
        $this->assign('picList',$lists);
    }
    //户型
    private function getRoomType($id,$type_id=0){
        $field = 'id,house_id,title,room,ting,chu,wei,yang,img,jianzhu_area';
        $lists = model('house_type')->where(['house_id'=>$id])->field($field)->select();
        $room = [];
        $data = [];
        $all  = [];
        if($lists){
            foreach($lists as $v){
                $total = isset($room[$v['room']])?$room[$v['room']]['total']:0;
                $room[$v['room']] = ['name'=>$v['room'],'total'=>$total+1];
                $data[$v['room']][] = $v;
                $all[]                = $v;
            }
        }
        $all_count = count($all);
        ksort($room);ksort($data);
        if($type_id && array_key_exists($type_id,$data)){
            $all = $data[$type_id];
        }
        $this->assign('type_id',$type_id);
        $this->assign('type_count',$all_count);
        $this->assign('room_title',$room);
        $this->assign('room_list',$data);
        $this->assign('room_all',$all);
    }

    //沙盘
    private function getProperty($id){
        $pro_img = model('house_property_pic')->where(['house_id'=>$id])->find();
        $this->assign('property_pic',$pro_img);
    }
    //楼盘详细
    private function getHouseInfo($id){
        $info = model('house')->where(['id'=>$id,'status'=>1])->find()->toArray();
        $info_data = model('house_data')->where(['id'=>$id])->find()->toArray();
        return array_merge($info,$info_data);
    }
    //楼盘相册 切换图
    private function getHousePic($id){
        $lists = model('house_pic')->where(['house_id'=>$id,'status'=>1])->field('img,cate_name')->group('cate_id')->select();
        $this->assign('slidePic',$lists);
    }
    private function getLists(){
        $where = $this->search();
        $sort  = $this->order();
        $field = 'id,title,img,house_status_name,money,tags_name,pic_count,open_time,complate_time,city_name,area_name,zhuangxiu_name,is_group,sale_phone,youhui,group_youhui,special_youhui,average_price,price,address';
        $lists = model('house')->where($where)->field($field)->order($sort)->paginate(10);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
    }
    private function search(){
        $param['area'] = input('param.area',0);
        $param['price']      = input('param.price',0);
        $param['tags']       = input('param.tags',0);
        $param['room']       = input('param.room',0);//户型
        $param['type']       = input('param.type',0);//楼盘类型
       // $param['status']     = input('param.status',0);//楼盘状态
        $param['renovation'] = input('param.renovation',0);//装修情况
        //$param['floor']      = input('param.floor',0);//楼层
        $param['group']      = input('param.group/d',0);//是否团购
        $param['sort']       = input('param.sort/d',0);//排序
        $data['status']      = 1;
        $keyword             = input('get.keyword');
        $data['city']        = $this->cityInfo['id'];
        if($keyword){
            $param['keyword'] = $keyword;
           $data['title'] = ['like','%'.$keyword.'%'];
        }
        if(!empty($param['area'])){
            $data['area'] = $param['area'];
        }
        if(!empty($param['price'])){
            $data['average_price'] = $this->getPrice($param['price']);
        }
        if(!empty($param['tags'])){
            $data[] = ['exp',"find_in_set({$param['tags']},tags_id)"];
        }
        if(!empty($param['room'])){
            $data[] = ['exp',"find_in_set({$param['room']},room_type)"];
        }
        if(!empty($param['type'])){
            $data[] = ['exp',"find_in_set({$param['type']},house_type)"];
        }
       // if(!empty($param['floor'])){
       //     $data[] = ['exp',"find_in_set({$param['floor']},floor_type)"];
       // }
        //if(!empty($param['status'])){
        //    $data['house_status'] = $param['status'];
       // }
        if(!empty($param['renovation'])){
            $data['zhuangxiu_id'] = $param['renovation'];
        }
        if(!empty($param['group'])){
            $data['is_group'] = $param['group'];
        }
        //$search = $param;
       // $current_param = array_filter($param);
        $dis_group = $param['group'] == 0 ? 1 : 0;
        $this->assign('group',$dis_group);
        $this->assign('param',$param);
        $this->assign('search',$param);
        return $data;
    }
   private function getAttr($return=''){
        $tags     = cache('linkmenu_5');//特色标签
        $status   = cache('linkmenu_1');//楼盘状态
        $room     = cache('linkmenu_6');//户型类型
        $type     = cache('linkmenu_7');//楼盘类型
        $floor    = cache('linkmenu_8');//楼层类型
        $renovation = cache('linkmenu_9');//装修程度
        if($return){
            $temp = cache('attr_menu');
            if(!$temp){
                $data = array_merge($tags['cate'],$status['cate'],$room['cate'],$type['cate'],$floor['cate'],$renovation['cate']);
                $temp = [];
                foreach($data as $v){
                    $temp[$v['id']] = $v;
                }
                cache('attr_menu',$temp,3600);
            }
          return $temp;
        }else{
            $this->assign('tags',$tags['cate']);
            $this->assign('status',$status['cate']);
            $this->assign('room',$room['cate']);
            $this->assign('type',$type['cate']);
            $this->assign('floor',$floor['cate']);
            $this->assign('renovation',$renovation['cate']);
        }
    }
    private function getPrice($key=''){
        return getHousePrice($key);
    }
    //排序
    private function order(){
        $sort = input('param.sort/d',0);
        switch($sort){
            case 1:
                $order = 'price asc';
                break;
            case 2:
                $order = 'price desc';
                break;
            default:
                $order = 'ordid asc,id desc';
                break;
        }
        //$dis_sort = $sort == 1 ? 2 : 1;
        $this->assign('sort',$sort);
        return $order;
    }
//户型排序
    private function huxingorder(){
        $sort = input('param.sort/d',0);
        switch($sort){
            case 1:
                $order = 'h.price asc';
                break;
            case 2:
                $order = 'h.price desc';
                break;
            case 3:
                $order = 'h.jianzhu_area asc';
                break;
            case 4:
                $order = 'h.jianzhu_area desc';
                break;
            default:
                $order = 'l.ordid asc,l.id desc';
                break;
        }
        //$dis_sort = $sort == 1 ? 2 : 1;
        $this->assign('sort',$sort);
        return $order;
    }
}